﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using nyxwin.Main.Base.Interfaces;
using nyxwin.Common.Providers;
using Microsoft.Phone.Shell;
using nyxwin.Main.ViewModel.SubModels;

namespace nyxwin.Main.View.SubViews
{
    public partial class Discussion : PhoneApplicationPage, ISupportErrors, ISupportProgress
    {
        private ProgressIndicator _progressIndicator = null;

        public Discussion()
        {
            InitializeComponent();

            this.Unloaded += new RoutedEventHandler(Discussion_Unloaded);
        }

        void Discussion_Unloaded(object sender, RoutedEventArgs e)
        {
            if (this.Model != null)
                this.Model.Unload();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            int id;

            if (NavigationContext.QueryString.ContainsKey("id") && int.TryParse(NavigationContext.QueryString["id"], out id))
            {
                if (this.Model != null)
                    this.Model.Unload();

                this.Model = new DiscussionViewModel(id);
                this.DataContext = this.Model;
            }
        }

        #region ISupportErrors Members

        public void RaiseError(string message)
        {
            MessageBox.Show(message, LocalizationProvider.Instance.GetString("Error"), MessageBoxButton.OK);
        }

        #endregion

        #region ISupportProgress Members

        public void SetInDeterminate(Guid id, string text)
        {
            if (_progressIndicator == null)
            {
                _progressIndicator = new ProgressIndicator();
                SystemTray.SetProgressIndicator(this, _progressIndicator);
            }

            _progressIndicator.IsIndeterminate = true;
            _progressIndicator.Text = text;
            _progressIndicator.IsVisible = true;
        }

        public void UnSetInDeterminate(Guid id)
        {
            if (_progressIndicator != null)
            {
                _progressIndicator.IsVisible = false;
            }
        }

        #endregion

        public DiscussionViewModel Model { get; private set; }

        private void NyxIcon_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            DependencyObject depObj = sender as DependencyObject;

            if (depObj != null)
            {
                ContextMenu cm = ContextMenuService.GetContextMenu(depObj);

                if (cm != null && cm.Parent == null)
                {
                    cm.IsOpen = true;
                }
            }
        }
    }
}